package com.rfid.service;

import java.io.File;
import java.io.FileOutputStream;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;

import org.apache.log4j.Logger;

import com.rfid.client.SettingWindow;
import com.rfid.service.exception.ClientException;
import com.teatrace.model.TraceInfo;

public class RFIDSaveInfoService {
	
	private SettingWindow settingWindow;
	private static final Logger log = Logger.getLogger(RFIDSaveInfoService.class);
	private static TraceInfo info = null;
	public RFIDSaveInfoService() {
	}
	
	public RFIDSaveInfoService(SettingWindow settingWindow) {
		this.settingWindow = settingWindow;
	}
	
	public void saveInObject() {
		info = new TraceInfo();
		String number = settingWindow.getNumberText().getText();
		String address = settingWindow.getAddressText().getText();
		String recordTime = settingWindow.getRecordTimeText().getText();
		String description = settingWindow.getDescriptionText().getText();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date time = null;
		try {
			time = (Date) sdf.parse(recordTime);
		} catch (ParseException e) {
			log.error("RFIDWriteService transport String to Date have an Exception, because:"+e.getMessage());
		}
		info.setNumber(number);
		info.setAddress(address);
		info.setRecordTime(time);
		info.setDescription(description);
		Properties prop = new Properties();
		
		String path = this.getClass().getClassLoader().getResource("").getPath();
		prop.setProperty("NUMBER", number);
		prop.setProperty("ADDRESS", address);
		prop.setProperty("RECORETIME", recordTime);
		prop.setProperty("DESCRIPTION", description);
		FileOutputStream out;
		try {
			out = new FileOutputStream(new File(path+"settingAttribute.properties"));
			prop.store(out,"");
		} catch (Exception e) {
			log.error(e);
			throw new ClientException(e);
		} 
	}

	public static TraceInfo getInfo() {
		return info;
	}
	public static void setInfo(TraceInfo info) {
		RFIDSaveInfoService.info = info;
	}
}
